﻿namespace HB.Base
{
    /// <summary>
    /// 单例模式基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SingleInstance<T> where T : new()
    {
        private static readonly object locker = 0;
        private static T instance = default(T);
        /// <summary>
        /// 单例实例对象
        /// </summary>
        public static T Instance
        {
            get
            {
                if (instance == null) //避免每次加锁判定，第一次需要
                {
                    lock (locker) // 锁定，防止多线程同时访问
                    {
                        if (instance == null) //双重判定
                        {
                            instance = new T();
                        }
                    }
                }
                return instance;
            }
            private set { }
        }

    }
}
